package com.yic.module.wms.service.warehouselocation;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.system.enums.dict.SystemBooleanEnum;
import com.yic.module.wms.dal.dataobject.warehousearea.WarehouseAreaDO;
import com.yic.module.wms.service.warehousearea.WarehouseAreaService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.wms.controller.admin.warehouselocation.vo.*;
import com.yic.module.wms.dal.dataobject.warehouselocation.WarehouseLocationDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.wms.convert.warehouselocation.WarehouseLocationConvert;
import com.yic.module.wms.dal.mysql.warehouselocation.WarehouseLocationMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.wms.enums.ErrorCodeConstants.*;

/**
 * 库位 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class WarehouseLocationServiceImpl implements WarehouseLocationService {

    @Resource
    private WarehouseLocationMapper warehouseLocationMapper;
    @Resource
    private WarehouseAreaService warehouseAreaService;

    @Override
    public Long createWarehouseLocation(WarehouseLocationCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getCode(), createReqVO.getAreaId());
        // 插入
        WarehouseLocationDO warehouseLocation = WarehouseLocationConvert.INSTANCE.convert(createReqVO);
        warehouseLocationMapper.insert(warehouseLocation);
        // 返回
        return warehouseLocation.getId();
    }

    @Override
    public void updateWarehouseLocation(WarehouseLocationUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getCode(), updateReqVO.getAreaId());
        // 更新
        WarehouseLocationDO updateObj = WarehouseLocationConvert.INSTANCE.convert(updateReqVO);
        warehouseLocationMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseLocation(Long id) {
        // 校验存在
        validateWarehouseLocationExists(id);
        // 删除
        warehouseLocationMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, String code, Long areaId) {
        validateCodeExists(id, code);
        validateOpenLocation(areaId);
    }

    private void validateCodeExists(Long id, String code) {
        WarehouseLocationDO warehouseLocationDO = warehouseLocationMapper.selectByCode(code);
        if (ObjectUtil.isEmpty(warehouseLocationDO)) {
            return;
        }
        if (id == null) {
            throw exception(WAREHOUSE_LOCATION_CODE_EXISTS);
        }
        if (!id.equals(warehouseLocationDO.getId())) {
            throw exception(WAREHOUSE_LOCATION_CODE_EXISTS);
        }
    }

    private void validateOpenLocation(Long areaId) {
        WarehouseAreaDO warehouseArea = warehouseAreaService.getWarehouseArea(areaId);
        if (warehouseArea.getOpenLocation().equals(SystemBooleanEnum.NO.getSystemBoolean())) {
            throw exception(WAREHOUSE_LOCATION_NOT_CHANGE);
        }
    }

    private void validateWarehouseLocationExists(Long id) {
        if (warehouseLocationMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_LOCATION_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseLocationDO getWarehouseLocation(Long id) {
        return warehouseLocationMapper.selectById(id);
    }

    @Override
    public List<WarehouseLocationDO> getWarehouseLocationList(Collection<Long> ids) {
        return warehouseLocationMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WarehouseLocationDO> getWarehouseLocationPage(WarehouseLocationPageReqVO pageReqVO) {
        return warehouseLocationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseLocationDO> getWarehouseLocationList(WarehouseLocationExportReqVO exportReqVO) {
        return warehouseLocationMapper.selectList(exportReqVO);
    }

    @Override
    public List<WarehouseLocationDO> getWarehouseLocationListByAreaId(Long id) {
        return warehouseLocationMapper.selectListByAreaId(id);
    }

    @Override
    public List<WarehouseLocationDO> getWarehouseLocationList(Collection<Long> ids, Long areaId, Collection<Integer> status) {
        return warehouseLocationMapper.selectList(ids, areaId, status);
    }

}
